//package com.book.bookshop.web;
//
//import com.book.bookshop.entity.Files;
//import com.book.bookshop.entity.User;
//import com.book.bookshop.service.UploadService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.util.FileCopyUtils;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.servlet.ModelAndView;
//
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.Objects;
//
//@Controller
//@RequestMapping("/upload")
//public class UploadController {
//    @Autowired
//    private UploadService uploadService;
//
//
//    /**
//     * 头像上传
//     *
//     * @return
//     */
//    /**
//     * 头像添加
//     */
//    @PostMapping("/save")
//    @ResponseBody
//    public ModelAndView savePhoto(Files files, MultipartFile images) {
//        ModelAndView mv = new ModelAndView();
//        String path = "D:\\workplace\\book-shop\\src\\main\\resources\\static\\images\\"; // 保存路径
//        String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
//        String suffix = Objects.requireNonNull(images.getOriginalFilename()).substring(images.getOriginalFilename().lastIndexOf("."));
//
//        try {
//            // Spring提供了文件操作类FileCopyUtils
//            FileCopyUtils.copy(images.getInputStream(), new FileOutputStream(path + fileName + suffix));
//
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//            System.out.println("上传失败！");
//            files.setImage(fileName + suffix);
//        }
//        System.out.println("上传成功！");
//        uploadService.save(files);
//        mv.setViewName("redirect:/book/index");
//        return mv;
//    }
//}